package com.kye.express.business.welfare_integral.viewbinder;

import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.databinding.ItemIntegralCenterListRecordObtainBinding;
import com.kye.model.bean.integral.IntegralItemType;
import com.kye.model.bean.integral.IntegralRecordObtainRecordBean;

import io.reactivex.functions.Consumer;
import me.drakeet.multitype.ItemViewBinder;

/**
 * @author : Joe
 * @version : 1.0
 * @editor : Joe
 * @created : 12/01/2018
 * @updated : 12/01/2018
 * @description : <Description>
 * @update_reason : <UpdateReason>
 */

public class IntegralRecordObtainItemViewBinder extends ItemViewBinder<IntegralRecordObtainRecordBean, IntegralRecordObtainItemViewBinder.ViewHolder> {

    private IntegralRecordObtainItemViewBinder.onItemListener onItemListener;

    public IntegralRecordObtainItemViewBinder(IntegralRecordObtainItemViewBinder.onItemListener onItemListener) {
        this.onItemListener = onItemListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_integral_center_list_record_obtain, parent, false);
        return new ViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull IntegralRecordObtainRecordBean item) {
        holder.bindData(item);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private IntegralRecordObtainRecordBean item;
        private ItemIntegralCenterListRecordObtainBinding binding;

        public ViewHolder(View itemView) {
            super(itemView);
            binding = DataBindingUtil.bind(itemView);
            RxView.clicks(binding.container)
                    .compose(new DebounceObservableTransformer<Object>())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            if (onItemListener != null) {
                                onItemListener.onItemIntegralObtainClicked(item);
                            }
                        }
                    });
        }

        public void bindData(IntegralRecordObtainRecordBean item) {
            this.item = item;
            binding.setData(item);
            if (item.getSignIntegral() > 0) {
                binding.integralValue.setText("+" + item.getSignIntegral());
            } else {
                binding.integralValue.setText("" + item.getSignIntegral());
            }
            String integralType = item.getIntegralType();
            if (IntegralItemType.BUSINESS.getCode().equals(integralType) || IntegralItemType.PHOTO.getCode().equals(integralType)) {
                binding.arrow.setVisibility(View.VISIBLE);
            } else {
                binding.arrow.setVisibility(View.INVISIBLE);
            }
            String reason = item.getReason();
            binding.integralObtainContent.setVisibility(!TextUtils.isEmpty(reason) ? View.VISIBLE : View.GONE);
        }
    }

    public interface onItemListener {
        void onItemIntegralObtainClicked(IntegralRecordObtainRecordBean item);
    }
}
